<?php
namespace app\home\controller;
use pidan\App;
use pidan\Request;
#use pidan\captcha\Captcha;
use pidan\captcha\facade\Captcha;
use pidan\facade\Session;
use pidan\facade\Db;
use app\admin\model\Admin;
use app\home\Controller;
use pidan\exception\ValidateException;


class Test extends Controller
{
	protected $app;
	public function __construct(){
		$this->app=app();
	}
	public function app(){

		return print_r(app(),true);
	}
	public function data(){
		0 ?:$data=2;
    return $data;
  }
	public function array_flip(){
		$msg='正确';
		if(!in_array(7,array_flip(lang('sortstyle')[1])))
			$msg='不正确';
		return print_r($msg,true);
  }
	public function in_array(){
		var_dump(in_array(0,[1,2,3,4]));
    return;
  }
	public function ext(){
    return request()->pathinfo();
  }
	public function param(){
		?>
		<!DOCTYPE html>
		<html>
		<head>
			<meta charset="utf-8">
			<meta name="viewport" content="width=device-width, initial-scale=1">
			<title>开始使用 layui</title>
		</head>
		<body>


		<script>
			//能存取数值 bool 字符与对象 数组，null保存不处理还是之前值
			session=function(k,v){//存取localStorage
				return v==undefined ? JSON.parse(sessionStorage.getItem(k)) : sessionStorage.setItem(k,JSON.stringify(v));
			}
			session('aaaaa', [null,{a:1111},{b:2222}]);
			console.log(typeof session('aaaaa'));
			console.log(!session('aaaaa')[3]);
			location.hash='/aaaa/cccb?asdfas=bbbb';
		</script>
		</body>
		</html>
		<?php
	}

  public function layer(){
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>开始使用 layui</title>
	<link href="/seller/res/layui/css/layui.css" rel="stylesheet">
</head>
<body>

<!-- 你的 HTML 代码 -->

<script src="/seller/res/layui/layui.js">
<script>
layui.use(['layer', 'form'], function(){
	var layer = layui.layer;
	var form = layui.form;

	layer.msg('Hello World');
		layer.ready(function(){
		var iii = layer.load(1, {shade: [0.1,'#fff']});
		setTimeout(function(){layer.close(iii)}, 3000);
	});

});
</script>
</body>
</html>
<?php
	}
	public function json(){
		$str='[{
    "title": "主页"
    ,"icon": "layui-icon-home"
    ,"list": [{
      "title": "控制台"
      ,"jump": "/"
    }, {
      "name": "homepage1"
      ,"title": "主页一"
      ,"jump": "home/homepage1"
    }, {
      "name": "homepage2"
      ,"title": "主页二"
      ,"jump": "home/homepage2"
    }]
  }, {
    "name": "component"
    ,"title": "组件"
    ,"icon": "layui-icon-component"
    ,"list": [{
      "name": "grid"
      ,"title": "栅格"
      ,"list": [{
        "name": "list"
        ,"title": "等比例列表排列"
      },{
        "name": "mobile"
        ,"title": "按移动端排列"
      },{
        "name": "mobile-pc"
        ,"title": "移动桌面端组合"
      },{
        "name": "all"
        ,"title": "全端复杂组合"
      },{
        "name": "stack"
        ,"title": "低于桌面堆叠排列"
      },{
        "name": "speed-dial"
        ,"title": "九宫格"
      }]
    }, {
      "name": "button"
      ,"title": "按钮"
    }, {
      "name": "form"
      ,"title": "表单"
      ,"list": [{
        "name": "element"
        ,"title": "表单元素"
      },{
        "name": "group"
        ,"title": "表单组合"
      }]
    }, {
      "name": "nav"
      ,"title": "导航"
    }, {
      "name": "tabs"
      ,"title": "选项卡"
    }, {
      "name": "progress"
      ,"title": "进度条"
    }, {
      "name": "panel"
      ,"title": "面板"
    }, {
      "name": "badge"
      ,"title": "徽章"
    }, {
      "name": "timeline"
      ,"title": "时间线"
    }, {
      "name": "anim"
      ,"title": "动画"
    }, {
      "name": "auxiliar"
      ,"title": "辅助"
    }, {
      "name": "layer"
      ,"title": "通用弹层"
      ,"list": [{
        "name": "list"
        ,"title": "功能演示"
      },{
        "name": "special-demo"
        ,"title": "特殊示例"
      },{
        "name": "theme"
        ,"title": "风格定制"
      }]
    }, {
      "name": "laydate"
      ,"title": "日期时间"
      ,"list": [{
        "name": "demo1"
        ,"title": "功能演示一"
      },{
        "name": "demo2"
        ,"title": "功能演示二"
      },{
        "name": "theme"
        ,"title": "设定主题"
      },{
        "name": "special-demo"
        ,"title": "特殊示例"
      }]
    },{
      "name": "table-static"
      ,"title": "静态表格"
      ,"jump": "component/table/static"
    }, {
      "name": "table"
      ,"title": "表格组件"
      ,"list": [{
        "title": "表格综合演示"
      },{
        "name": "auto"
        ,"title": "列宽自动分配"
      },{
        "name": "data"
        ,"title": "赋值已知数据"
      },{
        "name": "tostatic"
        ,"title": "转化静态表格"
      },{
        "name": "resetPage"
        ,"title": "自定义分页"
      },{
        "name": "radio"
        ,"title": "开启单选框"
      },{
        "name": "cellEdit"
        ,"title": "双击单元格编辑"
      },{
        "name": "form"
        ,"title": "加入表单元素"
      },{
        "name": "style"
        ,"title": "设置单元格样式"
      },{
        "name": "fixed"
        ,"title": "固定列"
      },{
        "name": "parseData"
        ,"title": "解析任意数据格式"
      },{
        "name": "onrow"
        ,"title": "行事件"
      },{
        "name": "search"
        ,"title": "外部搜索"
      },{
        "name": "initSort"
        ,"title": "设置初始排序"
      },{
        "name": "cellEvent"
        ,"title": "单元格事件"
      },{
        "name": "thead"
        ,"title": "复杂表头"
      }]
    }, {
      "name": "laypage"
      ,"title": "分页"
      ,"list": [{
        "name": "demo1"
        ,"title": "功能演示一"
      },{
        "name": "demo2"
        ,"title": "功能演示二"
      }]
    }, {
      "name": "dropdown"
      ,"title": "下拉菜单"
    }, {
      "name": "upload"
      ,"title": "上传"
      ,"list": [{
        "name": "demo1"
        ,"title": "功能演示一"
      },{
        "name": "demo2"
        ,"title": "功能演示二"
      }]
    }, {
      "name": "transfer"
      ,"title": "穿梭框"
    }, {
      "name": "tree"
      ,"title": "树形组件"
    }, {
      "name": "colorpicker"
      ,"title": "颜色选择器"
    }, {
      "name": "slider"
      ,"title": "滑块组件"
    }, {
      "name": "rate"
      ,"title": "评分"
    }, {
      "name": "carousel"
      ,"title": "轮播"
    }, {
      "name": "flow"
      ,"title": "流加载"
    }, {
      "name": "util"
      ,"title": "工具"
    }, {
      "name": "code"
      ,"title": "代码修饰"
    }]
  }, {
    "name": "template"
    ,"title": "页面"
    ,"icon": "layui-icon-template"
    ,"list": [{
      "name": "personalpage"
      ,"title": "个人主页"
      ,"jump": "template/personalpage"
    },{
      "name": "addresslist"
      ,"title": "通讯录"
      ,"jump": "template/addresslist"
    },{
      "name": "caller"
      ,"title": "客户列表"
      ,"jump": "template/caller"
    },{
      "name": "goodslist"
      ,"title": "商品列表"
      ,"jump": "template/goodslist"
    },{
      "name": "msgboard"
      ,"title": "留言板"
      ,"jump": "template/msgboard"
    },{
      "name": "search"
      ,"title": "搜索结果"
      ,"jump": "template/search"
    },{
      "name": "reg"
      ,"title": "注册"
      ,"jump": "user/reg"
    },{
      "name": "login"
      ,"title": "登入"
      ,"jump": "user/login"
    },{
      "name": "forget"
      ,"title": "忘记密码"
      ,"jump": "user/forget"
    },{
      "name": "404"
      ,"title": "404 页"
      ,"jump": "template/tips/404"
    },{
      "name": "error"
      ,"title": "出错页"
      ,"jump": "template/tips/error"
    }, {
      "name": ""
      ,"title": "内嵌页面"
      ,"spread": true
      ,"list": [{
        "name": ""
        ,"title": "iframe - Bing"
        ,"jump": "/iframe/link/demo"
      }]
    }]
  }, {
    "name": "app"
    ,"title": "应用"
    ,"icon": "layui-icon-app"
    ,"list": [{
      "name": "content"
      ,"title": "内容系统"
      ,"list": [{
        "name": "list"
        ,"title": "文章列表"
      },{
        "name": "tags"
        ,"title": "分类管理"
      },{
        "name": "comment"
        ,"title": "评论管理"
      }]
    },{
      "name": "forum"
      ,"title": "社区系统"
      ,"list": [{
        "name": "list"
        ,"title": "帖子列表"
      },{
        "name": "replys"
        ,"title": "回帖列表"
      }]
    },{
      "name": "message"
      ,"title": "消息中心"
    },{
      "name": "workorder"
      ,"title": "工单系统"
      ,"jump": "app/workorder/list"
    }]
  }, {
    "name": "senior"
    ,"title": "高级"
    ,"icon": "layui-icon-senior"
    ,"list": [{
      "name": "im"
      ,"title": "WebIM UI"
    },{
      "name": "echarts"
      ,"title": "Echarts"
      ,"list": [{
        "name": "line"
        ,"title": "折线图"
      },{
        "name": "bar"
        ,"title": "柱状图"
      },{
        "name": "map"
        ,"title": "地图"
      }]
    }]
  }, {
    "name": "user"
    ,"title": "用户"
    ,"icon": "layui-icon-user"
    ,"list": [{
      "name": "user"
      ,"title": "网站用户"
      ,"jump": "user/user/list"
    }, {
      "name": "administrators-list"
      ,"title": "后台管理员"
      ,"jump": "user/administrators/list"
    }, {
      "name": "administrators-rule"
      ,"title": "角色管理"
      ,"jump": "user/administrators/role"
    }]
  }, {
    "name": "set"
    ,"title": "设置"
    ,"icon": "layui-icon-set"
    ,"list": [{
      "name": "system"
      ,"title": "系统设置"
      ,"spread": true
      ,"list": [{
        "name": "website"
        ,"title": "网站设置"
      },{
        "name": "email"
        ,"title": "邮件服务"
      }]
    },{
      "name": "user"
      ,"title": "我的设置"
      ,"spread": true
      ,"list": [{
        "name": "info"
        ,"title": "基本资料"
      },{
        "name": "password"
        ,"title": "修改密码"
      }]
    }]
  }, {
    "name": "get"
    ,"title": "关于"
    ,"icon": "layui-icon-auz"
    ,"jump": "system/about"
  }]';
		$str=json_decode($str,true);var_export($str);
		return;
	}
	public function filter_var(){
		var_dump(filter_var('$d@qq.com', FILTER_VALIDATE_EMAIL));
		return;
	}
	public function column(){
		return var_export(Db::name('config')->column('value,id','name'),true);
	}
	public function server(){
		return print_r($_SERVER,true);
	}
	public function dbb(){
		var_dump(Db::query("select * from je_admin"));
		return bin2hex(random_bytes(8));
	}
	public function random_bytes(){
		$starttime = microtime(true);
		for ($i = 0; $i< 100*10000; $i++)
			bin2hex(random_bytes(8));
		echo"Static Time:  " , (microtime(true)-$starttime) ," ms\r\n";		
		return bin2hex(random_bytes(8));
	}
	public function ttl(){
		$key='g';
		$redis = new \Redis();
		$redis->connect('127.0.0.1', 6379);
		var_dump($redis->hmset($key,['a'=>'11']));
		$redis->expire($key,100);
		sleep(1);
		var_dump($redis->hget($key,'a'));
		echo "\r\n".$redis->ttl($key);exit;
	}
	public function speed(){
		$starttime = microtime(true);
		for ($i = 0; $i< 100*10000; $i++)
			now();
		echo"Static Time:  " , (microtime(true)-$starttime) ," ms\r\n";

		$starttime = microtime(true);
		for ($i = 0; $i< 100*10000; $i++)
			time();
		echo"Static Time:  " , (microtime(true)-$starttime) ," ms\r\n";
		exit;
	}
	public function hget(){
		var_dump(app('token')->get('admin_id'));
		exit;
	}
	public function doTest(){
		md5_clear('sdfsfsdf');
		$starttime = microtime(true);
		for ($i = 0; $i< 100*10000; $i++)
				md5_clear('sdfsfsdf');
		echo"Static Time:  " , (microtime(true)-$starttime) ," ms\r\n";


		$someObj=new \Base;
		$someObj->md5_clear('sdfsfsdf');
		// bench object method
		$starttime = microtime(true);
		for ($i = 0; $i< 100*10000; $i++)
				$someObj->md5_clear('sdfsfsdf');
		echo"Object Time:  " , (microtime(true)-$starttime) ," ms\r\n";

		// bench function
		md5_clear('sdfsfsdf');
		$starttime = microtime(true);
		for ($i = 0; $i< 100*10000; $i++)
				md5_clear('sdfsfsdf');
		echo"Function Time:" , (microtime(true)-$starttime) ," ms\r\n";
		exit;
		return;
	}
	public function microtime(){
			echo number_format(microtime(true),10);echo '<br>';
			echo number_format(microtime(true),10);echo '<br>';
			echo number_format(microtime(true),10);echo '<br>';
			echo number_format(microtime(true),10);echo '<br>';
			echo number_format(microtime(true),10);echo '<br>';
			echo number_format(microtime(true),10);echo '<br>';
			echo number_format(microtime(true),10);echo '<br>';
			echo number_format(microtime(true),10);echo '<br>';
			echo number_format(microtime(true),10);echo '<br>';
			return microtime(true);
	}
	public function request_time(){
			return $_SERVER['REQUEST_TIME'];
	}
	public function http_user_agent(){
			return $_SERVER['HTTP_USER_AGENT'];
	}
	public function ip(){
			return app('request')->ip();
	}
	public function scene()
	{
			$row=['username'=>'asdfsd', 'email'=>'', 'nickname'=>'sad', 'password'=>'asdfasdfdsasdfasddsasdfsasdfasdf', 'mobile'=>'1'];
			try {
					validate('app\admin\validate\Admin')->scene('add')->check($row);
			} catch (ValidateException $e) {
					// 验证失败 输出错误信息
					var_dump($e->getError());
			}
			return;
	}
	public function md5(){
			return substr(md5(microtime(true)),0,6);
	}
	public function error1(){
			return $this->error('ok中华人民共和国');
	}
	public function result1(){
			return $this->result('ok中华人民共和国');
	}
	public function success1(){
			return $this->success('ok中华人民共和国');
	}
	public function redirect1(){
			return $this->redirect('/');;
	}
	public function dbs(){
		$dbs=app('dbs');
		$row=$dbs->row('select * from {#je_#}admin where id=1');
			return print_r($row,true);
	}
	public function menu(){
		$a=['code'=>0,'msg'=>'','data'=>[['title'=>'主页','icon'=>'layui-icon-home','list'=>[['title'=>'控制台','jump'=>'/'],['name'=>'homepage1','title'=>'主页一','jump'=>'home/homepage1']]]]];
		print_r($a);
		return json_encode($a,JSON_UNESCAPED_UNICODE);
	}
	public function redis(){
	 //连接本地的 Redis 服务
	$redis = new \Redis();
	$redis->connect('127.0.0.1', 6379);
	echo "Connection to server successfully";
	//设置 redis 字符串数据
	$redis->set("tutorial-name", "Redis tutorial");
	// 获取存储的数据并输出
	echo "Stored string in redis:: " . $redis->get("tutorial-name");
	}
	public function cache(){
		cache('cache','cache_ok');
			#cache('cache',null);
			return var_dump(cache('cache'));
	}
	
	public function apcu5(){
			return print_r(apcu_key_info('z1_ts_4f7c2e19d3faf17e7b8e4a635adb61e7'),true);
	}
	public function apcu4(){
			return print_r(apcu_fetch('z1_ts_4f7c2e19d3faf17e7b8e4a635adb61e7'),true);
	}
	public function apcu3(){
		 //apcu_store('test',['test ok1'],3600);
			return print_r(apcu_fetch('test'),true);
	}
	public function apcu2(){
		if(apcu_clear_cache())
				return 'apcu已清空';
		else
				return 'apcu清空失败';
	}
	public function apcu1(){
		#apcu_clear_cache();
		$res=[];
		$row=apcu_cache_info();
		foreach($row['cache_list'] as $k=>$v){
			$res[]=$v['info'];
		}
		return print_r($res,true);
	}
	public function apcu(){
		//var_dump(apcu_fetch('zc10_cache_5e1684d395d7ad27c4e9ea537caecfe4'));
			return print_r(apcu_cache_info(),true);
	}
	public function admin(){
			$admin=Admin::find(1);

			return print_r($admin,true);
	}
	public function find(){
			$list=Admin::field('`id`, `username`, `nickname`, `loginfailure`, `logintime`, `loginip`, `createtime`,   `status`')->find(1);
			return $this->result(['code'=>0,'msg'=>'','count'=>100,'data'=>$list->logintime]);
	}
	public function select(){
			$list=Admin::field('`id`, `username`, `nickname`, `loginfailure`, `logintime`, `loginip`, `createtime`,   `status`')->select(1)->toArray();
			return $this->result(['code'=>0,'msg'=>'','count'=>100,'data'=>$list]);
	}
	public function db(){
			#$row=Db::table('je_user')->where('id', 1)->find();
			$row=Db::name('admin')->where('username','admin')->find();
		 return print_r($row,true);
	}
	public function phpinfo(){
		phpinfo();
		return ob_get_clean();
	}
	public function test_prs0(){
	 echo \Base::random();
			return ob_get_clean();
	}
	public function hello(){
		return 'hello world!';
	}
	public function verify()
	{
			#return (new Captcha($this->app->config,$this->app->session))->create();
			return Captcha::create();
	}
	public function session(){
			Session::set('name', 'thinkphp');
			return Session::get('name');
	}
	public function index(Request $request,App $app){
		include app_path().'/tpl/home.php';
		return ob_get_clean();
	}
	function need(){
		include app_path().'/tpl/home.php';

	}

}

